﻿
Imports osi.root
Imports osi.root.connector
Imports osi.root.utt
Imports osi.root.formation
Imports osi.root.utils
Imports osi.service.configuration
Imports c = osi.service.configuration

Public Class configuration_test
    Inherits [case]

    Private Shared Function machine_type() As String
        If Environment.ProcessorCount() > 7 Then
            Return "great"
        ElseIf Environment.ProcessorCount() > 3 Then
            Return "powerful"
        ElseIf Environment.ProcessorCount() > 2 Then
            Return "strange"
        ElseIf Environment.ProcessorCount() > 1 Then
            Return "normal"
        Else
            Return "lowend"""
        End If
    End Function

    Private Shared Function s() As section
        Return c.default(constants.file)("section")
    End Function

    Public Overrides Function run() As Boolean
        config_writer.write()
        assert_load(constants.file)

        assert_equal(s()("buildmode"), envs.build)

        assert_equal(s()("machine_type"), machine_type())

        assert_equal(s()("key1", create_variants("spf", "!pattern1")), "pattern1")
        assert_equal(s()("key1", create_variants("spf", "p!attern2")), "pattern2")
        assert_equal(s()("key1", create_variants("spf", "pa!ttern3")), "pattern3")
        assert_equal(s()("key1", create_variants("spf", "pattern4")), "key1 no match")

        assert_equal(s()("key2", create_variants("spcf", "!pattern1")), "pattern1")
        assert_equal(s()("key2", create_variants("spcf", "p!attern2")), "pattern2")
        assert_equal(s()("key2", create_variants("spcf", "pa!ttern3")), "pattern3")
        assert_equal(s()("key2", create_variants("spcf", "pattern4")), "key2 no match")

        assert_equal(s()("key3", create_variants("sf", "string1")), "string1")
        assert_equal(s()("key3", create_variants("sf", "string2")), "string2")
        assert_equal(s()("key3", create_variants("sf", "string3")), "string3")
        assert_equal(s()("key3", create_variants("sf", "string4")), "key3 no match")

        assert_equal(s()("key4", create_variants("iif", rnd_int(0, 100))).to_int32(), 100)
        assert_equal(s()("key4", create_variants("iif", rnd_int(100, 200))).to_int32(), 200)
        assert_equal(s()("key4", create_variants("iif", rnd_int(200, 300))).to_int32(), 300)
        assert_equal(s()("key4", create_variants("iif", rnd_int(300, 400))).to_int32(), 400)

        assert_equal(s()("key5", create_variants("ssf", "string1")), "string1,string2,string3")
        assert_equal(s()("key5", create_variants("ssf", "string2")), "string1,string2,string3")
        assert_equal(s()("key5", create_variants("ssf", "string3")), "string1,string2,string3")
        assert_equal(s()("key5", create_variants("ssf", "string4")), "string4,string5,string6")
        assert_equal(s()("key5", create_variants("ssf", "string5")), "string4,string5,string6")
        assert_equal(s()("key5", create_variants("ssf", "string6")), "string4,string5,string6")
        assert_equal(s()("key5", create_variants("ssf", "string7")), "string7,string8,string9")
        assert_equal(s()("key5", create_variants("ssf", "string8")), "string7,string8,string9")
        assert_equal(s()("key5", create_variants("ssf", "string9")), "string7,string8,string9")
        assert_equal(s()("key5", create_variants("ssf", "string10")), "string10")

        assert_equal(s()("key6", create_variants("dcf", rnd_double(0.1, 100.1))).to_double(), 100.1)
        assert_equal(s()("key6", create_variants("dcf", rnd_double(100.2, 200.2) + 0.1)).to_double(), 200.2)
        assert_equal(s()("key6", create_variants("dcf", rnd_double(200.3, 300.3) + 0.1)).to_double(), 300.3)
        assert_equal(s()("key6", create_variants("dcf", rnd_double(300.4, 400.4) + 0.1)).to_double(), 400.4)
        assert_equal(s()("key6", create_variants("dcf", rnd_double(400.5, 500.5))).to_double(), 500.5)
        assert_equal(s()("key6", create_variants("dcf", rnd_double(-100, -1))).to_double(), 500.5)
        assert_equal(s()("key6", create_variants("dcf", 0)).to_double(), 500.5)
        assert_equal(s()("key6", create_variants("dcf", 100.1)).to_double(), 500.5)
        assert_equal(s()("key6", create_variants("dcf", 200.2)).to_double(), 500.5)
        assert_equal(s()("key6", create_variants("dcf", 300.3)).to_double(), 500.5)

        assert_equal(s()("write_times").to_int32(), 1)

        config_writer.write()
        sleep()
        assert_equal(s()("write_times").to_int32(), 2)

        assert_true(do_(Function() As Boolean
                            IO.File.Delete(constants.file)
                            Return True
                        End Function,
                        False))
        Return True
    End Function
End Class
